﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;
using YiSha.Model.Result.QixingManage;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-09-17 22:30
    /// 描 述：业务类
    /// </summary>
    public class StoreOrderMasterBLL
    {
        private StoreOrderMasterService storeOrderMasterService = new StoreOrderMasterService();

        #region 获取数据
        public async Task<TData<List<StoreOrderMasterEntity>>> GetList(StoreOrderMasterListParam param)
        {
            TData<List<StoreOrderMasterEntity>> obj = new TData<List<StoreOrderMasterEntity>>();
            obj.Data = await storeOrderMasterService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<StoreOrderMasterEntity>>> GetPageList(StoreOrderMasterListParam param, Pagination pagination)
        {
            TData<List<StoreOrderMasterEntity>> obj = new TData<List<StoreOrderMasterEntity>>();
            obj.Data = await storeOrderMasterService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<StoreOrderMasterEntity>> GetEntity(long id)
        {
            TData<StoreOrderMasterEntity> obj = new TData<StoreOrderMasterEntity>();
            obj.Data = await storeOrderMasterService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }

        public TData<StoreOrderDto> GetGoodsOrderEntity(long id)
        {
            TData<StoreOrderDto> obj = new TData<StoreOrderDto>();
            var data = storeOrderMasterService.GetGoodsOrderEntity(id);
            obj.Data = data;

            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(StoreOrderMasterEntity entity)
        {
            TData<string> obj = new TData<string>();
            await storeOrderMasterService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await storeOrderMasterService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<string>> SaveShippingProductForm(StoreOrderMasterEntity entity)
        {
            TData<string> obj = new TData<string>();

            if (entity.IsNullOrZero())
            {
                obj.Message = "订单异常";
                obj.Tag = 0;
                return obj;
            }

            var orderinfo = storeOrderMasterService.GetEntity(entity.Id.Value).Result;

            if (orderinfo == null)
            {
                obj.Message = "订单异常";
                obj.Tag = 0;
                return obj;
            }

            //减库存改
            var des = await storeOrderMasterService.BaseRepository().FindList<StoreOrderDetailEntity>(t => t.order_id == entity.Id);

            foreach (var item in des)
            {
                var pwids = TextHelper.SplitToArray<long>(item.productids, ',');

                var pws = storeOrderMasterService.BaseRepository().FindList<StoreGoodsProductEntity>(t => pwids.Contains(t.Id.Value)).Result.ToList();

                foreach (var pw in pws)
                {
                    if (pw != null && pw.goods_type == 1)
                    {
                        pw.status = 1;
                    }
                    else
                    {
                        pw.status = 2;
                    }
                    await storeOrderMasterService.BaseRepository().Update(pw);
                }
            }

            //确认收货状态：0->未确认；1->已确认
            await storeOrderMasterService.SaveForm(orderinfo);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
